﻿/*******************************************************************************
 * Copyright © 2016-2020 北京网非科技有限公司 版权所有
 * Author: 北京网非科技有限公司
 * Description: 网非快速开发平台NET
 * Website：http://www.wfdot.com
*********************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using CubicFWNet.Application.SystemManage;
using CubicFWNet.Mvc.Filter;
using CubicFWNet.Mvc.Models;
using CubicFWNet.Common;
using CubicFWNet.Data.SystemManage.Model;
using Webdiyer.WebControls.Mvc;

namespace CubicFWNet.Mvc.Areas.SystemManage.Controllers
{
    public class ModuleController : ControllerBase
    {
        private ModuleApp _moduleApp;

        public ModuleController()
        {
            _moduleApp = new ModuleApp();
        }
        //[Authenticate]
        [HttpGet]
        public ActionResult Index(int id = 1)
        {
            ViewBag.Modules = _moduleApp.GetList();
            int pageSize = Application.UserProfile.GetPageSize(this.CurrentUserId);
            var result = _moduleApp.Load().ToPagedList<Module>(id, pageSize);
            //var result = new List<Module> { }.ToPagedList<Module>(id, pageSize);             
            if (Request.IsAjaxRequest())
            {
                //System.Threading.Thread.Sleep(2000);  //用于测试加载特效
                return PartialView("_AjaxModuleTable", result);
            }
            return View(result);
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetFormJson(Guid id)
        {
            var data = _moduleApp.GetForm(id);
            return Content(data.ToJson());
        }
        [Delete]
        [HttpPost]
        [HandlerAjaxOnly]
        public string Delete(Guid id)
        {
            return _moduleApp.Delete(id).ToJson();
        }

        //添加或修改角色
        [HttpPost]
        [HandlerAjaxOnly]
        [Insert]
        public string Save(Module module)
        {
            module.CreatorId = this.CurrentUserId;
            Result.Data = module.TODescriptionJson();
            if (!ModelState.IsValid)
            {
                Result.Status = ResultType.error.ToString();
                Result.Message = this.GetFirstModelError();
                return Result.ToJson();
            }
            else
            {               
                _moduleApp.AddOrUpdate(module);
                return Result.ToJson();
            }
        }
    }
}